package org.dev.vlp.utils;

import org.dev.vlp.config.mail.BaseMailConfig;
import org.dev.vlp.constants.MailEnum;
import org.dev.vlp.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Description
 * Param
 * return
 * Author:86184
 * Date: 2023/1/3
 */
@Component
public class MailUtils {
    final
    JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String mailFrom;


    public MailUtils(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    /**
     * 发送邮件基础方法
     * @param recipients 收件人数组
     * @param title 邮件标题
     * @param content 邮件正文
     * @param isHtml 是否html格式
     */
    private void sendMail(String[] recipients, String title, String content, boolean isHtml) {
        MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage, true, "utf-8");
            messageHelper.setFrom(mailFrom, "[越南单词本-极速版]");
            messageHelper.setTo(recipients);
            messageHelper.setSubject(title);
            messageHelper.setText(content, isHtml);
            javaMailSender.send(mimeMailMessage);
        } catch (MessagingException | UnsupportedEncodingException e) {
            throw new ApiException("邮件发送出错！");
        }
    }


    /**
     * 生成邮箱验证码，码长6位，字母与数字混合
     * @return 6位验证码
     */
    public String getVerificationCode() {
        // 增大数字权重，去除部分相似字母
        final String CHARACTERS = "0123456789012345678901234567890123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            sb.append(CHARACTERS.charAt((int) (Math.random() * (CHARACTERS.length()))));
        }
        return sb.toString();
    }

//    public void sendVerificationCode(String recipient, String code) {
//        String title = "[越南单词本-极速版]邮箱验证";
//        String url = hostAddress + "/user/checkVerificationCode?code=" + code;
//        String template = "你正在注册越南单词本小程序，请点击以下链接验证邮箱<a href= %s>%s</a>";
//        String content = String.format(template, url, url);
//        this.sendMail(new String[]{recipient}, title, content, true);
//    }
    public String sendVerificationCode(String mailTo, BaseMailConfig mailConfig, String code) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("verifyCode", code);
        variables.put("email", mailTo);
        MimeMessage mimeMessage = initMail(mailTo, mailConfig, variables);
        if (mimeMessage == null) {
            return MailEnum.CODE_GEN_FAIL.getMsg();
        }
        javaMailSender.send(mimeMessage);
        return MailEnum.SENT_SUCCESS.getMsg();
    }

    private MimeMessage initMail(String mailTo, BaseMailConfig mailConfig, Map<String, Object> variables) {
        Context context = new Context();
        context.setVariables(variables);
        context.setVariable("timeout", mailConfig.getTimeout() / 60);
        String emailContent = templateEngine.process(mailConfig.getTemplate(), context);
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            //MimeMessageHelper 对邮件进行配置，MimeMessageHelper 是一个邮件配置的辅助工具类
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailConfig.getFrom());
            helper.setText(emailContent, true);
            helper.setSubject(mailConfig.getSubject());
            helper.setTo(mailTo);
            return message;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
